package com.itheima;

import com.itheima.util.JedisUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;

public class Service {

    //控制单元
    public void service(String id){
        Jedis jedis= JedisUtils.getJedis();
        String value = jedis.get("compid:" + id);

        try{
            if (value==null){
                //不存在,创建该值
                jedis.setex("compid:"+id,300,Long.MAX_VALUE-10+"");
            }else {
                //存在,自增,条用业务
                Long var = jedis.incr("compid:" + id);
                business(id,10-(Long.MAX_VALUE-var));
            }
        }catch (JedisDataException e){
            System.out.println("使用已经到达次数上限，请升级会员级别");
            return;
        }finally {
            jedis.close();
        }
    }

    //业务操作
    public void business(String id,Long var) {
        System.out.println("用户："+id+" 业务操作正在执行第"+var+"次");
    }
}

class MyThread extends Thread {
    Service sc = new Service();

    @Override
    public void run() {
        while (true) {
            sc.service("chuji");
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Main {
    public static void main(String[] args) {
        MyThread mt = new MyThread();
        mt.start();
    }
}
